import {Component, OnInit} from '@angular/core';
import {Subscription} from 'rxjs';
declare let $: any;
import {RequestClientService} from '../../apiServices/request-client.service'

@Component({
  selector: 'app-home',
  templateUrl: 'home.template.html',
  styleUrls: ['./home.style.scss'],
  providers: []
})
export class HomeComponent implements OnInit {
  public message: string;
  public language: string = 'en';
  public textConfig: any = {
    bannerButton: {
      zh: "了解更多", en: "Learn More"
    }
  };
  
  public banner: any;
  public situation: any;
  public solution: any;
  public strength: any;
  public whitepaper: any;
  public team: any;
  public roadmap: any;
  public tokenplan: any;
  public consolte: any;
  public partner: any;

  private subscribtion: Subscription;

  constructor(private request: RequestClientService) {
    this.language = request.language;
    this.subscribtion = this.request.request$.subscribe(obj => {
      // 当取回的数据类型为cates，则为对应的属性附值，并搜索所有对应的items
      let cates, searchIds=[];
      if (obj.name == 'cates') {
        cates = obj.data.data;
        cates.forEach(element => {
          element.items = []; // 为items存入做准备
          this[element.location] = element;
          searchIds.push(element._id);
        });
        this.loadItemsForIds(searchIds);
      }
      // 当取回的数据类型为items，将对应的items添加到cate中
      let items;
      if (obj.name == 'items') {
        items = obj.data.data;
        items.forEach(element => {
          switch (element.cate_id)
          {
            case this.banner._id:
            this.banner.items.push(element);
            break;
            case this.situation._id:
            this.situation.items.push(element);
            break;
            case this.solution._id:
            this.solution.items.push(element);
            break;
            case this.strength._id:
            this.strength.items.push(element);
            break;
            case this.team._id:
            this.team.items.push(element);
            break;
            case this.roadmap._id:
            this.roadmap.items.push(element);
            break;
            case this.tokenplan._id:
            this.tokenplan.items.push(element);
            break;
            case this.consolte._id:
            this.consolte.items.push(element);
            break;
            case this.partner._id:
            this.partner.items.push(element);
            break;
          }
        });
        // 修改roadmap.items为两个一组
        let roadArr = this.roadmap.items;
        let roadItems =[];
        for(var i=0,len=roadArr.length;i<len;i+=2){
          roadItems.push(roadArr.slice(i,i+2));
        }
        this.roadmap.items = roadItems;
        // 当items全部取回分配完成以后，初始化前台js特效
        setTimeout(()=>{
          // initialization of carousel
          $.HSCore.components.HSCarousel.init('.js-carousel');
  
          // initialization of go to section
          // $.HSCore.components.HSGoTo.init('.js-go-to');
  
          // initialization of header 需要移动到top-nav里
          $.HSCore.components.HSHeader.init($('#js-header'));
          $.HSCore.helpers.HSHamburgers.init('.hamburger');
        },500)
        // 初始化前台js特效完成
      }
    });
  }

  async loadItemsForIds(searchIds): Promise<any> {
    if (searchIds instanceof Array != true) throw Error("搜寻条件必须是一个数组！");
    let items;
    try {
      items = await this.request.requset({
        client: 'items',
        request: {
            clientName: 'find',
            params: { query:{cate_id:{$in:searchIds},$limit: 1000} }
        },
        Observables:{ObservableName:'items'}
      })
    }catch(e){

    }
  }

  ngOnInit() {
    this.message = 'Hello';
    this.request.requset({
        client: 'cates',
        request: {
            clientName: 'find',
            params: { query:{$limit: 50} }
        },
        Observables:{ObservableName:'cates'}
    }).then(success => {
        console.log("success",success);
    }).catch(e => {
        //console.log(e);
    });
  }
}
